<?php
namespace app\job;

use app\common\model\Config;
use app\common\model\Order;
use fast\Nuo;
use think\Db;
use think\Env;
use think\Exception;
use think\Log;
use think\queue\Job;

class Bill extends BaseJob
{

    /**
     * 发票申请
     * @param Job $job
     * @param $data
     */
    public function handle(Job $job, $data)
    {
        Log::info('发票开票申请开始');
        Log::info($data);
        Db::startTrans();
        try{
            $billInfo = \app\common\model\Bill::where(['id'=>$data['bill_id'],'member_id'=>$data['member_id'],'status'=>1])->find();
            if ($billInfo){
                $billInfo->invoiceDate = date('Y-m-d H:i:s');
                $tax_rate = Config::where(['name'=>'tax_rate'])->value('value');
                $saler_tel = Config::where(['name'=>'saler_tel'])->value('value');
                $clerk = Config::where(['name'=>'clerk'])->value('value');
                $saler_address = Config::where(['name'=>'saler_address'])->value('value');
                $nuo = new Nuo();
                $arr = $nuo->requestBillingNew([
                    'order' => [
                        'buyerName' => $billInfo->buyerName,
                        'buyerTaxNum' => $billInfo->buyerTaxNum,
                        'buyerTel' => $billInfo->buyerTel,
                        'buyerAddress' => $billInfo->buyerAddress,
                        'buyerAccount' => $billInfo->buyerAccount,
                        'salerTaxNum' => Env::get('nuonuo.Taxnum'),
                        'salerTel' => $saler_tel,
                        'salerAddress' => $saler_address,
                        'orderNo' => $billInfo->orderNo,
                        'invoiceDate' => $billInfo->invoiceDate,
                        'clerk' => $clerk,//开票员
                        'buyerPhone' => $billInfo->mobile,
                        'email' => $billInfo->email,
                        'callBackUrl'=>'https://xm.xmlhzx.cn/index/callback/nuo',
                        'invoiceType' => '1',
                        'invoiceLine' => 'pc',
                        'invoiceDetail' => [
                            'goodsName' => $billInfo->bill_content,
                            'price' => $billInfo->bill_money,
                            'num' => '1',
                            'withTaxFlag' => 1,
                            'taxRate' => $tax_rate,
                        ]
                    ]]);
                if ($arr['code'] == '0'){
                    throw new Exception('第三方开票接口请求失败');
                }elseif ($arr['code'] == 'E0000'){//开票成功
                    $billInfo->invoiceSerialNum = $arr['result']['invoiceSerialNum'];
                    $billInfo->status = 20;
                    $billInfo->save();
                }else{
                    $billInfo->status = 22;
                    $billInfo->reason = $arr['describe'];
                    $billInfo->save();
                }
            }
            Db::commit();
            Log::info('开票成功结束');
        }catch (Exception $e){
            Db::rollback();
            Log::error($e->getMessage());
            Log::info('开票失败结束');
            throw $e;
        }
    }









}